runOnStartup(async runtime => {
    window.addEventListener("minerTick", (e) => {
    })
    window.addEventListener("minerInit", (e) => {
        MINER_Animation_HookCarTakeHooktoStart.doMoveAnimation()
        MINER_Animation_HookCarTakeHooktoStart.getAnimationHookCarArrive()
    })
});
globalThis.MINER_Animation_HookCarTakeHooktoStart = {
    doMoveAnimation: function () {

        var hook = runtime.objects.ToolHook?.getFirstInstance()
        var hookRope = runtime.objects.rope?.getFirstInstance()
        var maxLightOP = 0.14
        var minLightOP = 0
        for (let Lights of runtime.objects.Light.instances()) {
            Lights.opacity = cc_GamewithMath.RandomNumfor2(minLightOP, maxLightOP)
        }
        hook.isVisible = false
        hookRope.isVisible = false
        /**
         * @type {ISpriteInstance}
         */
        var hookCar = runtime.objects.HookCar?.getFirstInstance()
        if (hookCar == null)
            return
        /***
         * @type {IMoveToBehaviorInstance}
         */
        var hookCarMoveBehavior = hookCar.behaviors.MoveTo
        var moveEndPosX
        var moveEndPosY

        var hookCarInitPosX
        var hookCarInitPosY

        window.addEventListener("mp_getarrived", function doCarMove() {
            moveEndPosX = hook.x
            moveEndPosY = hookCar.y
            hookCarInitPosX = hookCar.x 
            hookCarInitPosY = hookCar.y
            hookCarMoveBehavior.moveToPosition(moveEndPosX, moveEndPosY, true)
            window.removeEventListener("mp_getarrived", doCarMove)
        })
        //hookCarMoveBehavior.moveToPosition(946, 190, true)
        hookCarMoveBehavior.addEventListener("arrived", function getarrive() {
            if (hookCar.x == moveEndPosX && hookCar.y == moveEndPosY) {
                var e = new CustomEvent("HookCarArrive", {})
                window.dispatchEvent(e)
                hookCarMoveBehavior.moveToPosition(hookCarInitPosX, hookCarInitPosY)
            }
            if (hookCar.x == hookCarInitPosX) {
                //console.log("Animation is end")
                hookCarMoveBehavior.removeEventListener("arrived", getarrive)
            }
        })
    },
    getAnimationHookCarArrive: function () {
        window.addEventListener("HookCarArrive", () => {
            var Hook = runtime.objects.ToolHook?.getFirstInstance()
            var hookRope = runtime.objects.rope?.getFirstInstance()
            if (Hook == null || hookRope == null) return
            Hook.isVisible = true
            hookRope.isVisible = true
            var t = setInterval(() => {
                for (let Lights of runtime.objects.Light.instances()) {
                    if (Lights.opacity < 1) {
                        Lights.opacity += 0.005
                    }
                    if (Lights.opacity >= 1) {
                        clearInterval(t)
                    }
                }
            }, 50)
        })
    }
}
export default MINER_Animation_HookCarTakeHooktoStart